package sg.nus.iss.vmcs.gui;

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import sg.nus.iss.vmcs.controllers.StoreController;
import sg.nus.iss.vmcs.gui.elements.LabelledDisplayStoreViewerBuilder;
import sg.nus.iss.vmcs.gui.elements.StoreViewer;
import sg.nus.iss.vmcs.guilisteners.DoorListener;
import sg.nus.iss.vmcs.utils.Constant;
import sg.nus.iss.vmcs.utils.StoreType;

/**
 * Machinery Simulator UI for simulation of customer functions<br><br>
 * Modifications:
 * <li>31/12/2010 - Cleaned up Machinery Simulator UI</li>
 * <br><br>
 * @author Olivo Miotto
 * @author Philip Chew
 */
public class MachinerySimulatorPanel extends Dialog {

    /** Generated Serial ID */
	private static final long serialVersionUID = -4764483855083182094L;
	
	/** Store Viewer for Cash */
	private StoreViewer cashDisplay;
    
	/** Store Viewer for Products */
	private StoreViewer drinksDisplay;
    
	/** Checkbox indicating if door is locked */
	private Checkbox	doorDisplay;
    
	/** Reference to the Store Controller */
	private StoreController  storeCtrl;
	
	/**
	 * Creates an instance of the Machinery Simulator Panel
	 * @param frame Parent frame for the dialog
	 * @param machineCtrl Machinery Controller
	 */
	public MachinerySimulatorPanel(Frame frame, StoreController storeCtrl) {
		super(frame, Constant.MACHINE_BANNER, false);
		
		//Initializations for this class
		this.storeCtrl = storeCtrl;
		
		//Banner Label 
		Label lblBanner = new Label(Constant.MACHINE_BANNER);
		lblBanner.setFont(Constant.BANNER_FONT);
		lblBanner.setAlignment(Label.CENTER);
		
		//Coin Display
		LabelledDisplayStoreViewerBuilder csv = new LabelledDisplayStoreViewerBuilder(
			"Quantity of Coins Available: ", null, storeCtrl.getStore(StoreType.CASH));
				
		csv.createNewStoreViewer();
		csv.buildBanner();
		csv.setListenerType(this.storeCtrl, StoreType.CASH);
		csv.buildStoreViewerItems();
		
		//csv.buildListeners();
		cashDisplay = csv.getStoreViewer();
		
		//Drinks Display
		LabelledDisplayStoreViewerBuilder dsv = new LabelledDisplayStoreViewerBuilder(
			"Quantity of Drinks Available: ", null, storeCtrl.getStore(StoreType.DRINKS));
		
		dsv.createNewStoreViewer();
		dsv.buildBanner();
		dsv.setListenerType(this.storeCtrl, StoreType.DRINKS);
		dsv.buildStoreViewerItems();
		
		//dsv.buildListeners();
		drinksDisplay = dsv.getStoreViewer();
		
		//Door Is Closed
		doorDisplay = new Checkbox();
        doorDisplay.addItemListener(new DoorListener(storeCtrl.getMainController().getMachineryController()));
        doorDisplay.setLabel("Door Locked");
        
        //Panels and Layout
        Panel pnlNorth = new Panel();
        pnlNorth.add(lblBanner);
        
        Panel pnlCenter = new Panel();
        pnlCenter.setLayout(new GridLayout(0, 1));
        pnlCenter.add(cashDisplay);
        pnlCenter.add(drinksDisplay);
                
		Panel pnlSouth = new Panel();
		pnlSouth.add(doorDisplay);
		
		this.setLayout(new BorderLayout());
		this.add(pnlNorth, 	BorderLayout.NORTH);
		this.add(pnlCenter, BorderLayout.CENTER);
		this.add(pnlSouth, 	BorderLayout.SOUTH);
		this.setResizable(false);
		this.pack();
		addWindowAdapter();
	}
	
	/**
	 * Displays the Machine Simulator Panel
	 */
	public void display() {
		this.setVisible(true);
	}
	
	/**
	 * Removes the Machine Simulator Panel from being displayed
	 */
    public void closeDown() {
    	this.dispose();
    }
    
    /**
     * Sets the active state for the Machinery Simulator Panel
     * @param cp
     * @param state
     */
    public void setActive(boolean state) {
        cashDisplay.setActive(state);
        drinksDisplay.setActive(state);
    }
    
    /**
     * Sets the state of the Door of Machinery
     * If door is locked, the display is disabled. 
     * If door is unlocked, the display is enabled.
     * @param state True to lock door, false if otherwise.
     */
    public void setDoorState(boolean state) {
    	doorDisplay.setState(state);
    	this.setActive(! state);
   }
    
	/**
	 * Adds a window adapter to close down the system
	 */
	private void addWindowAdapter() {
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				storeCtrl.getMainController().getMachineryController().closeDown();
			}
		});
	}
}
